package offer;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

class offer32second {
}
class Solution322 {
    public static List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
//        List track = new ArrayList();
        if(root==null)
        {
            return new ArrayList<>();
        }
        queue.add(root);
        int len = queue.size();
        while (!queue.isEmpty())
        {
            List<Integer> track = new ArrayList();
            for (int i = 0; i < len; i++) {
                TreeNode temp = queue.poll();
                track.add(temp.val );
                if(temp.left!=null) queue.add(temp.left);
                if(temp.right!=null) queue.add(temp.right);
            }
            res.add(track);
            len = queue.size();
        }
    return res;
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(3);
        TreeNode node1 = new TreeNode(9);
        TreeNode node2 = new TreeNode(20);
        TreeNode node3 = new TreeNode(15);
        root.left = node1;
        root.right=  node2;
        node2.left = node3;
        List<List<Integer>> lists = levelOrder(root);
    }
}
